package com.lyk.jsbridge.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lyk.jsbridge.Constant;
import com.lyk.jsbridge.R;
import com.lyk.jsbridge.http.MyOkhttp;
import com.lyk.jsbridge.http.callback.StringNoDialogCallback;
import com.lyk.jsbridge.http.request.BaseRequest;
import com.lyk.jsbridge.httpmodel.ForgetBean;
import com.lyk.jsbridge.httpmodel.RegisterBean;
import com.lyk.jsbridge.httpmodel.RegisterCode;
import com.lyk.jsbridge.util.Regular;
import com.lyk.jsbridge.util.TimeCount;
import com.lyk.jsbridge.util.ToastUtil;
import com.lyk.jsbridge.util.VolleySingleton;
import com.lyk.jsbridge.vo.VolleyError;
import com.lyk.jsbridge.vo.toolbox.ImageRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/3/12.
 */

public class ForgetActivity extends BaseActivty2 {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_layout)
    RelativeLayout rlTopLayout;
    @BindView(R.id.user_phone_num)
    EditText userPhoneNum;
    @BindView(R.id.img_clear_user)
    ImageView imgClearUser;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.text_smscode)
    EditText textSmscode;
    @BindView(R.id.btn_send_mes)
    Button btnSendMes;
    @BindView(R.id.text_pwd)
    EditText textPwd;
    @BindView(R.id.text_repwd)
    EditText textRepwd;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.img_tupian)
    ImageView imgTupian;
    @BindView(R.id.img_pwd)
    ImageView imgPwd;
    @BindView(R.id.img_repwd)
    ImageView imgRepwd;
    @BindView(R.id.img_imgcaptcha)
    ImageView imgImgcaptcha;
    private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        ButterKnife.bind(this);
        timeCount = new TimeCount(60000, 100, btnSendMes);
        initcaptcha();

        userPhoneNum.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (userPhoneNum.getText().toString().equals("")) {
                    imgClearUser.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgClearUser.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (editPhone.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textPwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textRepwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!textPwd.getText().toString().equals(textRepwd.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });
        editPhone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (editPhone.getText().toString().equals("")) {
                    imgTupian.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgTupian.setVisibility(View.GONE);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (editPhone.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textPwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textRepwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!textPwd.getText().toString().equals(textRepwd.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });


        textSmscode.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (textSmscode.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (editPhone.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textPwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textRepwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!textPwd.getText().toString().equals(textRepwd.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });


        textPwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (textPwd.getText().toString().equals("")) {
                    imgPwd.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgPwd.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (editPhone.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textPwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textRepwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!textPwd.getText().toString().equals(textRepwd.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });

        textRepwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (textRepwd.getText().toString().equals("")) {
                    imgRepwd.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgRepwd.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (editPhone.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textPwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (textRepwd.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!textPwd.getText().toString().equals(textRepwd.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });
    }

    @OnClick({R.id.img_back, R.id.rl_back, R.id.tv_title, R.id.rl_top_layout, R.id.user_phone_num, R.id.img_clear_user, R.id.edit_phone, R.id.text_smscode, R.id.btn_send_mes, R.id.text_pwd, R.id.text_repwd, R.id.btn_next, R.id.img_tupian, R.id.img_pwd, R.id.img_repwd, R.id.img_imgcaptcha})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_title:
                break;
            case R.id.rl_top_layout:
                break;
            case R.id.user_phone_num:
                break;
            case R.id.img_clear_user:
                userPhoneNum.setText("");
                break;
            case R.id.edit_phone:
                break;
            case R.id.text_smscode:
                break;
            case R.id.btn_send_mes:
                //获取验证码
                if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {

//                    btnLogin.setEnabled(false);
//                    btnLogin.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    login();

                }

                break;
            case R.id.text_pwd:
                break;
            case R.id.text_repwd:
                break;
            case R.id.btn_next:
                repass();
                break;

            case R.id.img_tupian:
                editPhone.setText("");
                break;
            case R.id.img_pwd:
                textPwd.setText("");
                break;
            case R.id.img_repwd:
                textRepwd.setText("");
                break;

            case R.id.img_imgcaptcha:
                initcaptcha();
                break;
        }
    }

    public void repass() {

        String url = Constant.URL + "api/v1/system/forgetSubmit/" + userPhoneNum.getText().toString() + "/" + "1234/" + textPwd.getText().toString();

        MyOkhttp.get(url)
                .tag(this)
                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);

                        showLoading();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
//                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Gson gson = new Gson();
                        ForgetBean forgetBean = new ForgetBean();
                        forgetBean = gson.fromJson(s, ForgetBean.class);
                        if (forgetBean.getMeta().isSuccess() == false) {
                            Toast.makeText(ForgetActivity.this, forgetBean.getMeta().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            finish();
                        }

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.d(response.toString(), "333333333333");

                        Toast.makeText(ForgetActivity.this, "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);
                        dismissLoading();
                    }
                });
    }

    public void login() {
        String url = Constant.URL + "api/v1/system/getMobileCaptcha/" + editPhone.getText().toString() + "/0";

        MyOkhttp.get(url)
                .tag(this)

                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        timeCount.start();
                        showLoading();

                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
//                        Toast.makeText(ForgetActivity.this, "请求验证码成功", Toast.LENGTH_SHORT).show();


                        Gson gson = new Gson();
                        RegisterCode registerCode = new RegisterCode();
                        registerCode = gson.fromJson(s, RegisterCode.class);
                        if (registerCode.getMeta().isSuccess() == true) {
//                            timeCount.cancel();
//                            btn_send_mes.setClickable(true);
                            btnSendMes.setClickable(true);
                            ToastUtil.showCenterToast(ForgetActivity.this, registerCode.getMeta().getMessage());
                        } else {
                            ToastUtil.showCenterToast(ForgetActivity.this, registerCode.getMeta().getMessage());
                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(ForgetActivity.this, "请求验证码失败", Toast.LENGTH_SHORT).show();
                        Log.d(response.toString(), "333333333333");
//                        timeCount.cancel();
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);
                        dismissLoading();
//                        timeCount.cancel();
                    }
                });
    }


    private void initcaptcha() {
        // TODO Auto-generated method stub


        ImageRequest imageRequest = new ImageRequest(Constant.URL + "/captcha.png", new com.lyk.jsbridge.vo.Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                Log.d(response.toString(), "3333333333333333333");
                imgImgcaptcha.setImageBitmap(response);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new com.lyk.jsbridge.vo.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                imgImgcaptcha.setImageResource(R.mipmap.ic_launcher);
            }
        });
        VolleySingleton.getInstance(ForgetActivity.this).addToRequestQueue(imageRequest);

    }

}
